/**
 * amis-core v2.4.0
 * Copyright 2018-2022 fex
 */

import { __awaiter, __generator, __assign } from 'tslib';
import omit from 'lodash/omit';
import '../utils/helper.js';
import { evalExpression } from '../utils/tpl.js';
import 'amis-formula';
import 'moment';
import { createObject } from '../utils/object.js';
import { dataMapping } from '../utils/dataMapping.js';
import '../utils/filter.js';

// 循环动作执行状态
var LoopStatus;
(function (LoopStatus) {
    LoopStatus[LoopStatus["NORMAL"] = 0] = "NORMAL";
    LoopStatus[LoopStatus["BREAK"] = 1] = "BREAK";
    LoopStatus[LoopStatus["CONTINUE"] = 2] = "CONTINUE";
})(LoopStatus || (LoopStatus = {}));
// 存储 Action 和类型的映射关系，用于后续查找
var ActionTypeMap = {};
// 注册 Action
var registerAction = function (type, action) {
    ActionTypeMap[type] = action;
};
// 通过类型获取 Action 实例
var getActionByType = function (type) {
    return ActionTypeMap[type];
};
// 根据动作类型获取属性排除列表
var getOmitActionProp = function (type) {
    var omitList = [];
    switch (type) {
        case 'toast':
            omitList = [
                'msgType',
                'msg',
                'position',
                'closeButton',
                'showIcon',
                'timeout',
                'title'
            ];
            break;
        case 'alert':
            omitList = ['msg'];
            break;
        case 'confirm':
            omitList = ['msg', 'title'];
            break;
        case 'ajax':
            omitList = ['api', 'messages', 'options'];
            break;
        case 'setValue':
            omitList = ['value', 'index'];
            break;
        case 'copy':
            omitList = ['content', 'copyFormat'];
            break;
        case 'email':
            omitList = ['to', 'cc', 'bcc', 'subject', 'body'];
            break;
        case 'link':
            omitList = ['link', 'blank', 'params'];
            break;
        case 'url':
            omitList = ['url', 'blank', 'params'];
            break;
        case 'for':
            omitList = ['loopName'];
            break;
        case 'goPage':
            omitList = ['delta'];
            break;
        case 'custom':
            omitList = ['script'];
            break;
        case 'broadcast':
            omitList = ['eventName'];
            break;
        case 'dialog':
            omitList = ['dialog'];
            break;
        case 'drawer':
            omitList = ['drawer'];
            break;
        case 'reload':
            omitList = ['resetPage'];
            break;
    }
    return omitList;
};
var runActions = function (actions, renderer, event) { return __awaiter(void 0, void 0, void 0, function () {
    var _i, actions_1, actionConfig, actionInstrance;
    return __generator(this, function (_a) {
        switch (_a.label) {
            case 0:
                if (!Array.isArray(actions)) {
                    actions = [actions];
                }
                _i = 0, actions_1 = actions;
                _a.label = 1;
            case 1:
                if (!(_i < actions_1.length)) return [3 /*break*/, 4];
                actionConfig = actions_1[_i];
                actionInstrance = getActionByType(actionConfig.actionType);
                // 如果存在指定组件ID，说明是组件专有动作
                if (!actionInstrance && actionConfig.componentId) {
                    actionInstrance = getActionByType('component');
                }
                else if (actionConfig.actionType === 'url' ||
                    actionConfig.actionType === 'link' ||
                    actionConfig.actionType === 'jump') {
                    // 打开页面动作
                    actionInstrance = getActionByType('openlink');
                }
                // 找不到就通过组件专有动作完成
                if (!actionInstrance) {
                    actionInstrance = getActionByType('component');
                }
                // 这些节点的子节点运行逻辑由节点内部实现
                return [4 /*yield*/, runAction(actionInstrance, actionConfig, renderer, event)];
            case 2:
                // 这些节点的子节点运行逻辑由节点内部实现
                _a.sent();
                if (event.stoped) {
                    return [3 /*break*/, 4];
                }
                _a.label = 3;
            case 3:
                _i++;
                return [3 /*break*/, 1];
            case 4: return [2 /*return*/];
        }
    });
}); };
// 执行动作，与原有动作处理打通
var runAction = function (actionInstrance, actionConfig, renderer, event) { return __awaiter(void 0, void 0, void 0, function () {
    var mergeData, expression, preventDefault, stopPropagation, args, afterMappingData, actionData, data;
    var _a, _b, _c;
    return __generator(this, function (_d) {
        switch (_d.label) {
            case 0:
                mergeData = createObject(createObject(renderer.props.data.__super
                    ? createObject(renderer.props.data.__super, { event: event })
                    : { event: event }, renderer.props.data), event.data);
                expression = (_a = actionConfig.expression) !== null && _a !== void 0 ? _a : actionConfig.execOn;
                if (expression && !evalExpression(expression, mergeData)) {
                    return [2 /*return*/];
                }
                preventDefault = actionConfig.preventDefault &&
                    evalExpression(String(actionConfig.preventDefault), mergeData);
                stopPropagation = actionConfig.stopPropagation &&
                    evalExpression(String(actionConfig.stopPropagation), mergeData);
                args = dataMapping(actionConfig.args, mergeData, function (key) {
                    return ['adaptor', 'responseAdaptor', 'requestAdaptor'].includes(key);
                });
                afterMappingData = dataMapping(actionConfig.data, mergeData);
                actionData = args && Object.keys(args).length
                    ? omit(__assign(__assign({}, args), (afterMappingData !== null && afterMappingData !== void 0 ? afterMappingData : {})), getOmitActionProp(actionConfig.actionType))
                    : afterMappingData;
                data = args && !Object.keys(args).length && actionConfig.data === undefined // 兼容历史
                    ? {}
                    : actionData !== undefined
                        ? actionData
                        : event.data;
                (_b = console.group) === null || _b === void 0 ? void 0 : _b.call(console, "run action ".concat(actionConfig.actionType));
                console.debug("[".concat(actionConfig.actionType, "] action args, data"), args, data);
                return [4 /*yield*/, actionInstrance.run(__assign(__assign({}, actionConfig), { args: args, data: data }), renderer, event, mergeData)];
            case 1:
                _d.sent();
                console.debug("[".concat(actionConfig.actionType, "] action end event"), event);
                (_c = console.groupEnd) === null || _c === void 0 ? void 0 : _c.call(console);
                // 阻止原有动作执行
                preventDefault && event.preventDefault();
                // 阻止后续动作执行
                stopPropagation && event.stopPropagation();
                return [2 /*return*/];
        }
    });
}); };

export { LoopStatus, getActionByType, registerAction, runAction, runActions };
